

import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JLabel;

import com.mysql.jdbc.PreparedStatement;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;


public class ViewAssetUI extends JPanel implements ActionListener{

	private JLabel txtName;
	private JLabel txtOwner;
	private JLabel txtCustodian;
	private JLabel txtStorageLocation;
	private JLabel txtType;
	private JLabel txtMaintenance;
	private JLabel txtFinancial;
	private JLabel txtConfidentiality;
	private JLabel txtIntegrity;
	private JLabel txtAvailability;
	private JLabel txtClassification;
	private JLabel txtDateAcquired;
	private JLabel txtRetentionPeriod;
	
	private JComboBox<String> comboModifyAsset;

	private Asset currSelectedAsset;
	
	/**
	 * Create the panel.
	 */
	public ViewAssetUI() {
		setLayout(null);
		
		JLabel lblModifyAsset = new JLabel("Modify Asset: <Name> - <ID>");
		lblModifyAsset.setFont(new Font("SansSerif", Font.BOLD, 12));
		lblModifyAsset.setBounds(58, 15, 175, 16);
		add(lblModifyAsset);
		
		comboModifyAsset = new JComboBox<String>();
		comboModifyAsset.addActionListener(this);
		comboModifyAsset.setBounds(230, 13, 195, 20);
		add(comboModifyAsset);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblName.setBounds(58, 46, 52, 14);
		add(lblName);
		
		JLabel lblOwner = new JLabel("Owner:");
		lblOwner.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblOwner.setBounds(58, 99, 52, 14);
		add(lblOwner);
		
		JLabel lblCustodian = new JLabel("Custodian:");
		lblCustodian.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCustodian.setBounds(58, 154, 72, 14);
		add(lblCustodian);
		
		JLabel lblType = new JLabel("Type:");
		lblType.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblType.setBounds(309, 46, 52, 14);
		add(lblType);
		
		JLabel lblDateAcquired = new JLabel("Date Acquired:");
		lblDateAcquired.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblDateAcquired.setBounds(58, 267, 105, 14);
		add(lblDateAcquired);
		
		JLabel lblRetentionPeriod = new JLabel("Retention Period:");
		lblRetentionPeriod.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblRetentionPeriod.setBounds(58, 322, 148, 14);
		add(lblRetentionPeriod);
		
		JLabel lblMaintenanceSchedule = new JLabel("Maintenance Schedule:");
		lblMaintenanceSchedule.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblMaintenanceSchedule.setBounds(309, 77, 148, 14);
		add(lblMaintenanceSchedule);
		
		JLabel lblValue = new JLabel("Value:");
		lblValue.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblValue.setBounds(391, 154, 55, 24);
		add(lblValue);
		
		JLabel lblFinancial = new JLabel("Financial:");
		lblFinancial.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFinancial.setBounds(307, 190, 73, 14);
		add(lblFinancial);
		
		JLabel lblConfidentiality = new JLabel("Confidentiality:");
		lblConfidentiality.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblConfidentiality.setBounds(307, 220, 110, 14);
		add(lblConfidentiality);
		
		JLabel lblIntegrity = new JLabel("Integrity:");
		lblIntegrity.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblIntegrity.setBounds(307, 250, 73, 14);
		add(lblIntegrity);
		
		JLabel lblAvailability = new JLabel("Availability:");
		lblAvailability.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblAvailability.setBounds(307, 280, 73, 14);
		add(lblAvailability);
		
		JLabel lblClassification = new JLabel("Classification:");
		lblClassification.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblClassification.setBounds(309, 108, 93, 14);
		add(lblClassification);
		
		JLabel lblStorageLocation = new JLabel("Storage Location:");
		lblStorageLocation.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblStorageLocation.setBounds(58, 209, 125, 14);
		add(lblStorageLocation);
		
		txtName = new JLabel();
		txtName.setBounds(58, 68, 195, 26);
		add(txtName);
		
		txtOwner = new JLabel();
		txtOwner.setBounds(58, 123, 200, 25);
		add(txtOwner);
		
		txtCustodian = new JLabel();
		txtCustodian.setBounds(58, 178, 200, 25);
		add(txtCustodian);
		
		txtStorageLocation = new JLabel();;
		txtStorageLocation.setBounds(58, 233, 200, 25);
		add(txtStorageLocation);
		
		txtType = new JLabel();
		txtType.setBounds(464, 46, 110, 20);
		add(txtType);
		
		txtMaintenance = new JLabel();
		txtMaintenance.setBounds(464, 77, 110, 20);
		add(txtMaintenance);
		
		txtFinancial = new JLabel();
		txtFinancial.setBounds(464, 187, 110, 20);
		add(txtFinancial);
		
		txtConfidentiality = new JLabel();
		txtConfidentiality.setBounds(464, 217, 110, 20);
		add(txtConfidentiality);
		
		txtIntegrity = new JLabel();
		txtIntegrity.setBounds(464, 247, 110, 20);
		add(txtIntegrity);
		
		txtAvailability = new JLabel();
		txtAvailability.setBounds(464, 277, 110, 20);
		add(txtAvailability);
		
		txtClassification = new JLabel();
		txtClassification.setBounds(464, 108, 110, 20);
		add(txtClassification);
		
		txtDateAcquired = new JLabel("");
		txtDateAcquired.setBounds(58, 291, 203, 20);
		add(txtDateAcquired);
		
		txtRetentionPeriod = new JLabel("");
		txtRetentionPeriod.setBounds(58, 346, 203, 20);
		add(txtRetentionPeriod);
		
		setAssetNames();
	}
	
	public void setAssetNames() {
		ResultSet rs;
		String query = ("SELECT ID, Name from asset WHERE isDeleted = 0 ORDER BY Name");
		Connection con = DBConnector.getConnection();
		rs = DBConnector.executeQuery(con, query);
		try {
			comboModifyAsset.removeAllItems();
			while(rs.next())
				comboModifyAsset.addItem(rs.getString("Name")+"-"+rs.getString("ID"));
			
			con.close();
			if(comboModifyAsset.getSelectedItem() != null)
				currSelectedAsset = Asset.createAssetFromID(getSelectedID());
				//currSelectedAsset = Asset.createAssetFromName(comboModifyAsset.getSelectedItem().toString());
			else
				clearForm();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void clearForm()
	{
		txtName.setText("");
		txtOwner.setText("");
		txtCustodian.setText("");
		txtStorageLocation.setText("");		
		txtCustodian.setText("");
		txtType.setText("");
		txtMaintenance.setText("");
		txtFinancial.setText("");
		txtConfidentiality.setText("");
		txtIntegrity.setText("");
		txtAvailability.setText("");
		txtClassification.setText("");
	}
	
	private int getSelectedID()
	{
		StringTokenizer tokenizer = new StringTokenizer(comboModifyAsset.getSelectedItem().toString(), "-");
		tokenizer.nextToken(); //go past the name
		return Integer.parseInt(tokenizer.nextToken());
	}
	
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource() == comboModifyAsset  && comboModifyAsset.getSelectedItem() != null) {
			currSelectedAsset = Asset.createAssetFromID(getSelectedID());
			txtName.setText(currSelectedAsset.getName());
			txtOwner.setText(currSelectedAsset.getOwner());
			txtCustodian.setText(currSelectedAsset.getCustodian());
			txtType.setText(currSelectedAsset.getTypeDesc());
			txtMaintenance.setText(currSelectedAsset.getMaintenanceScheduleDesc());
			txtFinancial.setText(currSelectedAsset.getFinancialDesc());
			txtConfidentiality.setText(currSelectedAsset.getConfidentialityDesc());
			txtIntegrity.setText(currSelectedAsset.getIntegrityDesc());
			txtAvailability.setText(currSelectedAsset.getAvailabilityDesc());
			txtClassification.setText(currSelectedAsset.getClassificationDesc());
			txtStorageLocation.setText(currSelectedAsset.getStorageLocation());
			txtDateAcquired.setText(currSelectedAsset.getDateAcquired().toString());
			txtRetentionPeriod.setText(currSelectedAsset.getRetentionPeriod().toString());
		}	
	}
}
